package org.lgh.wallet.common;

import java.util.HashMap;

/**
 * 操作类型枚举
 *
 * @author wei-xhh
 * @date 2023-02-27
 */
public enum OperateTypeEnum {

    PAY("支出", 1),
    INCOME("收入", 2);

    OperateTypeEnum(String key, Integer value) {
        this.key = key;
        this.value = value;
    }

    private String key;
    private Integer value;

    private static HashMap<Integer, OperateTypeEnum> valueMap = new HashMap<Integer, OperateTypeEnum>();

    //静态代码块
    static {
        for (OperateTypeEnum item : OperateTypeEnum.values()) {
            valueMap.put(item.getValue(), item);
        }
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    // 前台传进来的值通过这个方法来转换为类型
    public static OperateTypeEnum getByValue(int value) {
        OperateTypeEnum result = valueMap.get(value);
        if (result == null) {
            throw new IllegalArgumentException("No element matches " + value);
        }
        return result;
    }

}
